package com.example.javaee_library.Service.Impl;/**
 * @author Miao-yongzheng
 * @create 2021-12-20 20:51
 */

import cn.hutool.Hutool;
import cn.hutool.core.convert.Convert;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.example.javaee_library.Entity.Appointment;
import com.example.javaee_library.Entity.Book;
import com.example.javaee_library.Entity.User;
import com.example.javaee_library.Service.User_Service;
import com.example.javaee_library.dao.AppointmentDao;
import com.example.javaee_library.dao.UserDao;
import com.example.javaee_library.dto.JsonData;
import com.example.javaee_library.exception.NoNumberException;
import com.example.javaee_library.exception.NoUserException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description:
 * @author: 34911
 * @time: 2021/12/20 20:51
 */
@Slf4j
@Service
public class Usere_Servicelmpl implements User_Service {
    @Autowired
    UserDao userDao;
    @Autowired
    AppointmentDao appointmentDao;

    @Override
    public User Login(String Username, String Password) {

            User user = userDao.login(Convert.toInt(Username),Password);
            return user;

    }

    @Override
    public List<Appointment> myAppoint(String studentId) {
        List<Appointment> appointment = appointmentDao.queryBystudentWithBook(Convert.toLong(studentId));
        return appointment;
    }

    @Override
    public List<User> Serach(String UserId) {
        System.out.println("查询了"+'%'+UserId+'%');
        List<User> result = userDao.Serach('%'+UserId+'%');
        return  result;
    }

    @Override
    public boolean InsertUser(int UserId, String password, String autority) {
        System.out.println("插入了人员");
        password = SecureUtil.md5(password);//md5加密一次
        int back = userDao.Insert(UserId,password,autority);
        if(back>0)
            return true;
        else
            throw new NoNumberException(121,"插入用户失败");
    }

    //用户自己修改
    @Override
    public boolean UpdateByUser(int UserId, String password) {
        User user = userDao.Serach_Byuserid(String.valueOf(UserId));
        if(user==null)
        {
            return false;
        }
        int back = 0;
        if(ObjectUtil.equals("admin",user.getAuthority()))
        {
            throw new NoNumberException(403,"权限错误,不能修改管理员信息");//错误类我就没定义用的noNumber
        }
        else
        {
            back = userDao.UpdatebyUser(UserId,password);
        }
        return back>0?true:false;
    }

    @Override
    public boolean UpdateByAdmin(int oldId, String password, String authority, int state, int deleted) {
       if(userDao.UpdatebyAdmin(oldId, password, authority, state, deleted)>0)
           return true;
       else
           return false;
    }

    @Override
    public boolean freeze(int id) {
        //冻结
        if(userDao.Changefreeze(id,1)>0)
            return true;
        else
            return false;
    }

    @Override
    public boolean unfreeze(int id) {
        //解冻
        if(userDao.Changefreeze(id,0)>0)
            return true;
        else
            return false;
    }


}
